/**
* @(#)demo.study.concurrent.Test.java
* 南京中兴软创科技有限责任公司
* @date 2013-12-5
*/
package demo.study.concurrent.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/** 
 * 
 * @author <a href="tong.hao@zte.com.cn">童浩</a>
 * @version 1.0 
 * @since 2013-12-5 下午3:09:23 
 */

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Thread[] ts = new Thread[1000];// only declare
		System.out.println(ts[2]);
		final Lock l = new ReentrantLock();
		final C c = new C(l);
		for(int i=0; i<ts.length; i++) {
			ts[i] = new Thread(new Runnable() {

				@Override
				public void run() {
					c.m();
				}
				
			});
			ts[i].start();
			c.f();
		}
	}

}

class C {
	
	Lock l;
	
	public C(Lock l) {
		this.l = l;
	}
	
	public void m() {
		// 必须是同一把锁才能锁住,锁住的是方法级别的
		l.lock();
		try {
		
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("c.enclosing_method()");
		} finally {
			l.unlock();
		}
	}
	
	public void f() {
		System.out.println("f");
	}
}
